package com.yummy.controller;



import com.yummy.common.BaseResponse;
import com.yummy.common.MD5Util;
import com.yummy.common.TokenUtils;

import com.yummy.entity.Employee;
import com.yummy.entity.User;
import com.yummy.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.TimeUnit;


@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserService userService;

    @Autowired
    TokenUtils tokenUtils;

    @Autowired
    private RedisTemplate redisTemplate;

    Logger logger = LoggerFactory.getLogger(UserController.class);

    @PostMapping("/addUser")
    public BaseResponse addUser(@RequestBody User user){
        System.out.println(user);
        int result = userService.addUser(user);

        if (result==1){
            return BaseResponse.success();
        }
        return BaseResponse.fail();
    }

    @PostMapping("/editUser")
    public BaseResponse updateUser(@RequestBody User user){
        int result = userService.modifyUser(user);

        if (result==1){
            return BaseResponse.success();
        }
        return BaseResponse.fail();
    }


    @PostMapping("/deleteUser")
    public BaseResponse deleteEmployee(@RequestBody User user){
        int result = userService.deleteUser(user.getId());
        if (result==1){
            return BaseResponse.success();
        }
        return BaseResponse.fail();
    }


    /* 前端用户登录 */
    @PostMapping("/login")
    public BaseResponse login(@RequestBody User user) throws NoSuchAlgorithmException {
        String username = user.getName();
        System.out.println(username);
        String password = user.getPassword();
        logger.info("登录信息==>name:"+username+"\tpassword:"+password);
        logger.info(MD5Util.getMD5(password));
        User loginUser = userService.login(username, MD5Util.getMD5(password));
        System.out.println(loginUser);
        //登录失败
        if (loginUser == null){

            return BaseResponse.fail().message("登录失败，用户名或密码错误");
        }else {
            String token = tokenUtils.getToken(loginUser.getId().toString(), password);
            logger.info("当前用户token:"+token);
            //缓存token和用户id
            redisTemplate.opsForValue().set(token,loginUser.getId().toString(),2, TimeUnit.HOURS);
            return BaseResponse.success().data("username",username).data("Authorization",token).data("userId",loginUser.getId());
        }
    }

    @PostMapping("/registerUser")
    public BaseResponse register(@RequestBody User user) throws NoSuchAlgorithmException {
        logger.info("注册人："+user.toString());

        user.setPassword(MD5Util.getMD5(user.getPassword()));
        //未注册店铺时店铺id为-1

        int result = userService.addUser(user);

        if (result==1){
            return BaseResponse.success().message("注册成功，快去登录吧");
        }
        return BaseResponse.fail().message("注册失败，请重试");
    }

    @PostMapping("/findUserData")
    public BaseResponse selectUser(HttpServletRequest request){

        User currentUser = tokenUtils.getCurrentUser(request);
        logger.info("根据token获取到的用户信息"+currentUser.toString());


        return BaseResponse.success().data("user",currentUser);



    }


}
